﻿using System;

class MyStack<T>
{
    public T[] Items;
    private int top;
    private int capacity;

    public MyStack(int capacity)
    {
        this.capacity = capacity;
        this.Items = new T[capacity];
        this.top = -1;
    }

    public void InitStack()
    {
        this.Items=new T[capacity];
        this.top = -1;
    }

    public void ClearStack()
    {
        this.top = -1;
    }

    public bool Push(T item)
    {
        if(top>=capacity-1)
        {
            Console.WriteLine("栈已满，无法执行入栈操作");
            return false;
        }
        top++;
        Items[top] = item;
        return true;
    }
    public T Pop()
    {
        if(top<0)
        {
            throw new InvalidOperationException("栈为空，无法执行出栈操作");
        }

        T item = Items[top];
        top--;
        return item;
    }
    public int Count
    {
        get { return top + 1; }
    }
}

class Program
{
    static void Main()
    {
        MyStack<int> intStack = new MyStack<int>(5);

        intStack.InitStack();

        intStack.Push(10);
        intStack.Push(20);
        intStack.Push(30);

        Console.WriteLine($"栈中元素数量: {intStack.Count}");

        Console.WriteLine($"出栈: {intStack.Pop()}");
        Console.WriteLine($"出栈: {intStack.Pop()}");

        intStack.ClearStack();

        Console.WriteLine($"清空后元素数量: {intStack.Count}");
    }
}